package com.example.webtest.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class restservice {

	@Autowired
	RestTemplate restTemplate;

	public String getInfo(){
		String url ="http://localhost:8081/info/cjs";
		JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
		String name = forObject.getString("name");
		return name;
	}


	public String postInfo(){
		String url ="http://localhost:8081/money";
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		MultiValueMap<String,String> form =new LinkedMultiValueMap<>();
		form.add("money","10000");
		form.add("money","200000");
		HttpEntity<MultiValueMap<String,String>>  httpEntity= new HttpEntity<>(form, httpHeaders);
		ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
		String body = stringResponseEntity.getBody();
		return body;
	}



}
